﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;

namespace ForkCarRelationManager
{
    public partial class FormAddClient : Form
    {
        ClientBI clientBI = new ClientBI();
        public FormAddClient()
        {
            InitializeComponent();
        }


        private void FormAddClient_Load(object sender, EventArgs e)
        {
            //查找所有分公司作为数据源
            DataTable companyTable = clientBI.initCompany();
            DataRow maintordr = companyTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            companyTable.Rows.InsertAt(maintordr, 0);
         //   TBCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBCompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ID";
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        //添加按钮
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (this.TBClientName.TextLength == 0 || this.TBClientName.Text == "")
            {
                MessageBox.Show("客户名称不能为空");
            }
            else if (this.TBContactName.TextLength == 0 || this.TBContactName.Text == "")
            {
                MessageBox.Show("联系人不能为空！");
            }
            else if (this.TBPhone.TextLength == 0 || this.TBContactName.Text == "")
            {
                MessageBox.Show("联系人电话不能为空！");
            }
            else if (TBSalers.SelectedValue == null || TBSalers.SelectedValue.ToString()=="0")
            {
                MessageBox.Show("业务员不能为空！");
            }
            else if (TBCompany.SelectedValue == null || TBCompany.SelectedValue.ToString()=="0")
            {
                MessageBox.Show("客户所属分公司不能为空！");
            }
            else
            {
              bool mark =  clientBI.addClient(TBClientName.Text, TBClientLocation.Text, TBCompany.SelectedValue.ToString(), TBContactName.Text, TBPosition.Text, TBPartment.Text, TBPhone.Text, TBSalers.Text, TBPlace.Text, TBMark.Text);
                   
                  
                   
                if (mark == true)
                {
                    MessageBox.Show("添加成功");
                    this.DialogResult = DialogResult.Yes;
                    //this.Hide();
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }

            }
        }

        //取消按钮
        private void btnResign_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //选择了分公司就刷新一下业务员列表
        private void TBCompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (TBCompany.SelectedValue.ToString() != "0")
            {
                String companystrid = this.TBCompany.SelectedValue.ToString();
                int companyId = Convert.ToInt32(companystrid);
                //请求业务员列表数据
                MaintorBI maintorBI = new MaintorBI();
                DataTable salers = maintorBI.findMaintorInCompany(companyId);
                DataRow maintordr = salers.NewRow();
                maintordr[0] = "0";
                maintordr[1] = "";
                salers.Rows.InsertAt(maintordr, 0);
               // TBSalers.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
                TBSalers.AutoCompleteSource = AutoCompleteSource.ListItems;
                TBSalers.DataSource = salers;
                this.TBSalers.DisplayMember = "MaintorName";
                this.TBSalers.ValueMember = "ID";
            }
            else
            {
                TBSalers.DataSource = null;
            }
        }

      

    }
}
